package com.lhkj.ct.meta.modules.gauge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.enums.DeleteStatus;
import com.lhkj.ct.base.enums.GlobalStatus;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.core.metadata.SqlOrder;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.meta.modules.gauge.mapper.DiagItemMapper;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblDiagItem;
import com.lhkj.ct.meta.modules.gauge.service.DiagItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 医嘱项目 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-11-02
 */
@Service
public class DiagItemServiceImpl extends IServiceImpl<DiagItemMapper, TblDiagItem> implements DiagItemService {

    @Override
    public IPage<TblDiagItem> selectPages(TblDiagItem diagItem, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDiagItem> queryWrapper = new LambdaQueryWrapper<TblDiagItem>()
                .like(StringUtils.isNotBlank(diagItem.getItemName()), TblDiagItem::getItemName, diagItem.getItemName())
                .like(StringUtils.isNotBlank(diagItem.getItemCode()), TblDiagItem::getItemCode, diagItem.getItemCode())
                .eq(null != diagItem.getStatus(), TblDiagItem::getStatus, diagItem.getStatus())
                .eq(Objects.nonNull(diagItem.getItemType()), TblDiagItem::getItemType, diagItem.getItemType())
                .eq(TblDiagItem::getDelFlag, DeleteStatus.NORMAL);
        return getBaseMapper().selectPages(
                generatePage(pageQuery, SqlOrder.asc(TblDiagItem::getSort)),
                queryWrapper
        );
    }

    @Override
    public List<Option<String>> listOptions(TblDiagItem diagItem) {
        LambdaQueryWrapper<TblDiagItem> queryWrapper = new LambdaQueryWrapper<TblDiagItem>()
                .like(StringUtils.isNotBlank(diagItem.getItemName()), TblDiagItem::getItemName, diagItem.getItemName())
                .eq( TblDiagItem::getStatus, GlobalStatus.NORMAL)
                .eq(Objects.nonNull(diagItem.getItemType()), TblDiagItem::getItemType, diagItem.getItemType())
                .eq(TblDiagItem::getDelFlag, DeleteStatus.NORMAL);
        return getBaseMapper().listOptions(queryWrapper);
    }

    @Override
    public boolean checkCodeUnique(TblDiagItem diagItem) {
        TblDiagItem target = getBaseMapper().selectOne(
                new LambdaQueryWrapper<TblDiagItem>()
                        .eq(TblDiagItem::getItemCode, diagItem.getItemCode())
                        .eq(TblDiagItem::getDelFlag, DeleteStatus.NORMAL)
                        .last("limit 1")
        );
        return (null == target || target.getId().equals(diagItem.getId()));
    }

    @Override
    public int insert(TblDiagItem entity) {
        if (null == entity.getSort()) {
            entity.setSort(getBaseMapper().selectMaxSort());
        }
        Validate.isTrue(checkCodeUnique(entity), "项目编码已存在！");
        return getBaseMapper().insert(entity);
    }
}
